package hotel.controller.order;

import javafx.fxml.FXML;

import javafx.scene.control.Button;
import hotel.common.MsgBox;
import hotel.modal.Customer;
import hotel.modal.Order;
import hotel.modal.Room;
import hotel.modal.RoomRent;
import javafx.event.ActionEvent;

import javafx.scene.control.TitledPane;
import javafx.stage.Stage;
import javafx.scene.control.Label;

public class OrderDetailController {
	@FXML
	private TitledPane roomPane;
	@FXML
	private Label durationLbl;
	@FXML
	private Label roomTypeLbl;
	@FXML
	private Label priceLbl;
	@FXML
	private Label startTimeLbl;
	@FXML
	private TitledPane custPane;
	@FXML
	private Label discountLbl;
	@FXML
	private Label creditLbl;
	@FXML
	private Label nameLbl;
	@FXML
	private Label genderLbl;
	@FXML
	private Label certIdLbl;
	@FXML
	private Label phoneLbl;
	@FXML
	private Label custTypeLbl;
	@FXML
	private TitledPane orderPane;
	@FXML
	private Label otherLbl;
	@FXML
	private Label basicLbl;
	@FXML
	private Label totalLbl;
	@FXML
	private Label realTotalLbl;
	@FXML
	private Button btnOk;
	
	protected Order order;
	protected RoomRent roomRent;
	protected Customer customer;
	protected Room room; 
	
	@FXML
	public void initialize() {
	}
	
	public Order getOrder() {
		return order;
	}
	
	public void setOrder(Order order) {
		this.order = order;
		if(order != null) {
			this.roomRent = order.getRoomRent();
			this.room = order.getRoom();
			this.customer = order.getCustomer();
		}
		showInfo();
	}
	
	private void showInfo() {
		if(order == null || room == null || customer == null ) {
			MsgBox.showErrorDialog("错误", null, "读取数据错误！");
			closeWindow();
		}
		
		roomPane.setText(String.format("入住房间资料（房间号：%s）", room.getRoomNo()));
		String roomType = room.getTypeName();
		if(room.isHourRoom()) roomType += "(钟点房)";
		roomTypeLbl.setText(roomType);
		String price = String.format("%.2f元/%s", room.getPrice(), room.isHourRoom()?"小时":"天");
		priceLbl.setText(price);
		durationLbl.setText(String.format("%d天", roomRent.getDuration()));
		startTimeLbl.setText(roomRent.getStartTime());
		nameLbl.setText(customer.getName());
		genderLbl.setText(customer.getGender());
		certIdLbl.setText(customer.getCertId());
		phoneLbl.setText(customer.getPhone());
		custTypeLbl.setText(customer.getTypeName());
		discountLbl.setText(String.format("%.2f", customer.getDiscountRate()));
		creditLbl.setText(String.format("%.2f", customer.getCredit()));
		roomPane.setText(String.format("入住房间资料（房间号：%s）", room.getRoomNo()));
		custPane.setText(String.format("客户资料（客户编号：%d）", customer.getCustomerNo()));
		
		basicLbl.setText(String.format("%.2f", order.getBasicCost()));
		otherLbl.setText(String.format("%.2f", order.getOtherCost()));
		totalLbl.setText(String.format("%.2f", order.getTotalCost()));
		realTotalLbl.setText(String.format("%.2f", order.getRealCharge()));
	}

	// Event Listener on Button[#btnOk].onAction
	@FXML
	public void onBtnOkAction(ActionEvent event) {
		closeWindow();
	}
	
	private void closeWindow() {
		Stage stage = (Stage)btnOk.getScene().getWindow();
		stage.close();
	}
}
